package com.example.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.result.Result;
import com.example.entity.mes.Sked;
import com.example.entity.mqtt.Datasc;
import com.example.entity.mqtt.Items;
import com.example.mapper.ItemsMapper;
import com.example.service.DatascService;
import com.example.mapper.DatascMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 14501
* @description 针对表【datasc】的数据库操作Service实现
* @createDate 2024-04-01 11:08:06
*/
@Service
@DS("mes")
public class DatascServiceImpl extends ServiceImpl<DatascMapper, Datasc>
    implements DatascService{

    @Resource
    private DatascMapper datascMapper;
    @Resource
    private ItemsMapper itemsMapper;

    @Override
    public Result queryIPageList(Integer current, Integer size) {
        try{
            IPage<Datasc> config = new Page<>(current, size);
            // 解决当搜索条件为空时，默认查询所有数据
            QueryWrapper<Datasc> maintainQueryWrapper = new QueryWrapper<>();
            IPage<Datasc> datascIPage = this.datascMapper.selectPage(config, maintainQueryWrapper);
            // 每页展示的数据
            List<Datasc> datascList = datascIPage.getRecords();
            // 将响应数据填充到map中
            if (datascIPage.getTotal()==0){
                return Result.error("暂无数据");
            }
            Map map = new HashMap();
            map.put("pages", datascIPage.getPages());
            map.put("total", datascIPage.getTotal());
            map.put("list", datascList);
            return Result.success(map);
        }catch (Exception e) {
            return Result.error("分页查询异常" + String.valueOf(e),"500");
        }
    }

    @Override
    public Result queryById(Long id) {
        try {
            List<Items> itemsList = this.itemsMapper.selectList(new QueryWrapper<Items>().eq("did", id));
            return Result.success(itemsList);
        }catch (Exception e){
            return Result.error("查询异常" + String.valueOf(e),"500");
        }
    }
}




